﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FarmacieDAL;
using Telerik.Windows.Controls;
using System.ComponentModel;

namespace Farmacie.Controls
{
    /// <summary>
    /// Interaction logic for RetetaControl.xaml
    /// </summary>
    public partial class RetetaControl : UserControl, INotifyPropertyChanged
    {
        private List<Substanta_Activa> toateSubstantele = Medicament.ToateSubstantele;
        private int[] toateTipCompensat = new int[] { 30, 50, 75, 90, 100 };
        private Substanta_Activa substantaSelectata;
        private Reteta reteta = new Reteta();

        public List<Substanta_Activa> ToateSubstantele
        {
            get { return toateSubstantele; }
        }

        public List<int> ToateTipCompensat
        {
            get { return toateTipCompensat.ToList(); }
        }

        public List<Substanta_Activa> SubstanteCompensate
        {
            get { return reteta.SubstanteCompensate; }
        }

        public Substanta_Activa SubstantaSelectata
        {
            get { return substantaSelectata; }
            set
            {
                if (value != substantaSelectata)
                {
                    substantaSelectata = value;

                    if (reteta.SubstanteCompensate.Contains(substantaSelectata))
                        buttonAdauga.Content = "Sterge";
                    else
                        buttonAdauga.Content = "Adauga";

                    OnPropertyChanged("SubstantaSelectata");
                }
            }
        }

        public Reteta Reteta
        {
            get { return reteta; }
            set 
            {
                if (value != reteta)
                {
                    reteta = value;
                    OnPropertyChanged("Reteta");
                    OnPropertyChanged("SubstanteCompensate");
                }
            }
        }

        public RetetaControl()
        {
            InitializeComponent();
            buttonAdauga.Click += new RoutedEventHandler(buttonAdauga_Click);
            Reteta = reteta;
            this.DataContext = this;
        }

        void buttonAdauga_Click(object sender, RoutedEventArgs e)
        {
            if (substantaSelectata != null)
            {
                if (reteta.SubstanteCompensate.Contains(substantaSelectata))
                    reteta.Detalii_Reteta.Remove(reteta.Detalii_Reteta.First(x => x.Id_SubstantaActiva == substantaSelectata.Id_SubstantaActiva));
                else
                    reteta.Detalii_Reteta.Add(new Detalii_Reteta() { Reteta = reteta, Substanta_Activa = substantaSelectata });

                SubstantaSelectata = null;

                OnPropertyChanged("SubstanteCompensate");
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
